package com.huidz.calendar.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.huidz.calendar.R;
import com.huidz.calendar.activityadapter.ShengRiAdapter;
import com.huidz.calendar.model.BWLModel;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.Url;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ShengRiActivity extends AppCompatActivity implements View.OnClickListener {

    private ListView home_list;
    private List<Fragment> fraglist;
    private ImageView comeback;
    private ArrayList<BWLModel.data> new_list_view;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.srb_listhead);
        inti();
    }
    public ShengRiAdapter myAdapter;
    /**
     * 初始化加载调用方法
     */
    public void inti() {
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        home_list = findViewById(R.id.srb_list);
        new_list_view = new ArrayList<>();
        myAdapter = new ShengRiAdapter(new_list_view,
                ShengRiActivity.this);
        home_list.setAdapter(myAdapter);
        gettianqi();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                onBackPressed();
                break;
        }
    }

    public void gettianqi() {
        OkHttpClient client = new OkHttpClient();
        //构造Request对象
        //采用建造者模式，链式调用指明进行Get请求,传入Get的请求地址
        Request request = new Request.Builder().get().url(Url.GETBWL + "?logintoken=" + Url.USER_TOKEN).build();
        Call call = client.newCall(request);
        //异步调用并设置回调函数
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, final Response response) {
                try {
                    final String responseStr;
                    responseStr = response.body().string();
                    Message message = new Message();
                    message.obj = responseStr;
                    gettq.sendMessage(message);
                } catch (Exception e) {
                    Apiutils.SetLog(e.toString() + "");
                    e.printStackTrace();
                }
            }
        });
    }

    public static BWLModel userms;
    public Handler gettq = new Handler() {
        public void handleMessage(Message msg) {
            String msgs = null;
            try {
                JSONObject success_Object = null;
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    msgs = success_Object.getString("msg");
                    if (code.equals("1")) {
                        Gson gson = new Gson();
                        userms = gson.fromJson(msg.obj.toString(),
                                BWLModel.class);
                        new_list_view.addAll(userms.getData());
                        myAdapter.notifyDataSetChanged();
                    } else
                        Apiutils.SetToast(ShengRiActivity.this , msgs + "");
                }

            } catch (
                    Exception e) {
                e.printStackTrace();
            }
        }
    };

}
